package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.*;
import com.ideal.manage.esms.bean.sys.Parameter;
import com.ideal.manage.esms.service.project.*;
import com.ideal.manage.esms.service.system.ParameterService;
import com.ideal.manage.esms.util.POIUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/indexManage")
public class IndexManageController {
    @Resource
    private ParameterInfoService parameterInfoService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private AlarmInfoService alarmInfoService;
    @Resource
    private EquipmentInfoService equipmentInfoService;
    @Resource
    private AccountPropertyService accountPropertyService;
    @Resource
    private ControlOrderService controlOrderService;
    @Resource
    private ThresholdManageService thresholdService;
    @Resource
    private CategoryManageService categoryManageService;

    //参数测点
    @RequestMapping("index_list")
    public void indexList(Model model/*,String Q_LIKE_name,String Q_EQ_objName,String Q_EQ_source,String Q_EQ_isShow*/) {
        /******条件搜索所需******/
        List<Parameter> objNames = parameterService.findChildByParentCode("1010");
        model.addAttribute("objNames", objNames);

        List<TargetManagement> sources = new ArrayList<>();
        TargetManagement targetSource = new TargetManagement();
        targetSource.setId(ParameterInfo.CAN);
        targetSource.setName("通信-can");
        sources.add(targetSource);
        TargetManagement targetSource1 = new TargetManagement();
        targetSource1.setId(ParameterInfo.MODBUS);
        targetSource1.setName("通信-modbus");
        sources.add(targetSource1);
        TargetManagement targetSource2 = new TargetManagement();
        targetSource2.setId(ParameterInfo.CAN_CAL);
        targetSource2.setName("通信-can-计算");
        sources.add(targetSource2);
        TargetManagement targetSource3 = new TargetManagement();
        targetSource3.setId(ParameterInfo.MODBUS_CAL);
        targetSource3.setName("通信-modbus-计算");
        sources.add(targetSource3);
        TargetManagement targetSource4 = new TargetManagement();
        targetSource4.setId(ParameterInfo.DIAGNOSE);
        targetSource4.setName("诊断");
        sources.add(targetSource4);
        model.addAttribute("sources", sources);
        /******查询全部*******/
       /* List<ParameterInfo> allParameterInfo = parameterInfoService.findAllParameterInfo();
        model.addAttribute("allParameterInfo", allParameterInfo);*/
    }

    /**
     * 参数测点数据回显
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("index_data")
    @ResponseBody
    public PageDto indexData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> page = parameterInfoService.findAllParameterInfos(pageNum, request);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 参数测点的数据删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("index_del")
    @ResponseBody
    public Result indexDel(String[] ids) {
        parameterInfoService.delParameterInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    //查询code
    @RequestMapping("index_code")
    @ResponseBody
    public String verfiyCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<ParameterInfo> list = parameterInfoService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }
        return null;
    }

    /**
     * 参数测点的数据增加
     *
     * @param model
     */
    @RequestMapping("index_add")
    public void indexAdd(Model model) {
        indexList(model);
        //所属类别
        List<TargetManagement> objTypes = new ArrayList<>();
        TargetManagement objType = new TargetManagement();
        objType.setId(1);
        objType.setName("电压");
        objTypes.add(objType);
        TargetManagement objType2 = new TargetManagement();
        objType2.setId(2);
        objType2.setName("能量");
        objTypes.add(objType2);
        TargetManagement objType3 = new TargetManagement();
        objType3.setId(3);
        objType3.setName("电流");
        objTypes.add(objType3);
        TargetManagement objType4 = new TargetManagement();
        objType4.setId(4);
        objType4.setName("温度");
        objTypes.add(objType4);
        TargetManagement objType5 = new TargetManagement();
        objType5.setId(5);
        objType5.setName("SOC");
        objTypes.add(objType5);
        TargetManagement objType6 = new TargetManagement();
        objType6.setId(6);
        objType6.setName("循环次数");
        objTypes.add(objType6);
        model.addAttribute("objTypes", objTypes);
        List<Parameter> propertyTypes = parameterService.findChildByParentCode("1000");
        model.addAttribute("propertyTypes", propertyTypes);

        //参数统计方法
        List<TargetManagement> aggsMethod = new ArrayList<>();
        TargetManagement aggs1 = new TargetManagement();
        aggs1.setId(1);
        aggs1.setName("累计");
        aggsMethod.add(aggs1);
        TargetManagement agg2 = new TargetManagement();
        agg2.setId(2);
        agg2.setName("今日");
        aggsMethod.add(agg2);
        TargetManagement agg3 = new TargetManagement();
        agg3.setId(3);
        agg3.setName("实时");
        aggsMethod.add(agg3);
        TargetManagement agg4 = new TargetManagement();
        agg4.setId(4);
        agg4.setName("最高");
        aggsMethod.add(agg4);
        TargetManagement agg5 = new TargetManagement();
        agg5.setId(5);
        agg5.setName("最低");
        aggsMethod.add(agg5);
        model.addAttribute("aggsMethod", aggsMethod);
        TargetManagement agg6 = new TargetManagement();
        agg6.setId(6);
        agg6.setName("极差");
        aggsMethod.add(agg6);
        model.addAttribute("aggsMethod", aggsMethod);
        TargetManagement agg7 = new TargetManagement();
        agg7.setId(7);
        agg7.setName("平均");
        aggsMethod.add(agg7);
        model.addAttribute("aggsMethod", aggsMethod);
    }

    /**
     * 参数测点的数据保存
     *
     * @param id
     * @param code
     * @param name
     * @param objName
     * @param objType
     * @param propertyType
     * @param unit
     * @param source
     * @param isShow
     * @return
     */
    @RequestMapping("index_save")
    @ResponseBody
    public Result indexSave(String id, String code, String name, Integer objName, Integer objType, Integer propertyType, String unit,
                            Integer source, Integer isShow, String esName, Long esAggsMethod) {
        parameterInfoService.saveParameterInfo(id, code, name, objName, objType, propertyType, unit, source, isShow, esName, esAggsMethod);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 参数测点的数据编辑
     *
     * @param id
     * @param model
     */
    @RequestMapping("index_edit")
    public void indexEdit(String id, Model model) {

        ParameterInfo parameterInfo = parameterInfoService.findById(id);
        model.addAttribute("parameterInfo", parameterInfo);

        indexAdd(model);
    }


    /**
     * 导入数据
     */
    @RequestMapping("index_importData")
    public void indexImportData(Model model) {


    }

    @RequestMapping("indexImport_Data")
    @ResponseBody
    public String indexImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return parameterInfoService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;

    }

    /**
     * 下载模板
     */
    @RequestMapping("index_exportTemplate")
    public void index_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "parameterInfoTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "使用对象", "所属类别", "类型", "单位", "来源", "是否展示", "接口查询字段名", "参数统计方法"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:C001");
        row1.createCell(1).setCellValue("例:电压/SOC...");
        row1.createCell(2).setCellValue("例:1:项目,2:电池包,3:电池模组,4:变流器,5:电池单体,6:平台(只需给出数字)");
        row1.createCell(3).setCellValue("例:1:电压,2:能量,3:电流,4:温度,5:SOC,6:循环次数(只需给出数字)");
        row1.createCell(4).setCellValue("例:1:整型,2:浮点型,3:字符型,4:日期型(只需给出数字)");
        row1.createCell(5).setCellValue("例:V/W/℃");
        row1.createCell(6).setCellValue("例:1:通信-can,2:通信-modbus ,3:通信-can-计算 ,4:通信-modbus-计算 ,5:诊断(只需给出数字)");
        row1.createCell(7).setCellValue("例:0:不展示,1:展示(只需给出数字)");
        row1.createCell(8).setCellValue("例:Cell_58_Volt ... ");
        row1.createCell(9).setCellValue("例:1.累计,2.今日,3.实时,4.最高,5.最低,6.极差,7.平均(只需给出数字)");


        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    /**
     * 导出数据
     */
    @RequestMapping("index_exportData")
    public void indexExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<ParameterInfo> parameterInfos = parameterInfoService.findAllParameterInfo();
        //设置要导出的文件的名字
        String fileName = "parameterInfo" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "名称", "使用对象", "所属类别", "类型", "单位", "来源", "是否展示", "接口查询字段名", "参数统计方法", "创建时间", "修改时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (ParameterInfo parameterInfo : parameterInfos) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(parameterInfo.getId());
            row1.createCell(1).setCellValue(parameterInfo.getCode());
            row1.createCell(2).setCellValue(parameterInfo.getName());
            row1.createCell(3).setCellValue(parameterInfo.getObjName());
            row1.createCell(4).setCellValue(parameterInfo.getObjType());
            row1.createCell(5).setCellValue(parameterInfo.getPropertyType());
            row1.createCell(6).setCellValue(parameterInfo.getUnit());
            row1.createCell(7).setCellValue(parameterInfo.getSource());
            row1.createCell(8).setCellValue(parameterInfo.getIsShow());
            row1.createCell(9).setCellValue(parameterInfo.getEsName());
            row1.createCell(10).setCellValue(parameterInfo.getEsAggsMethod());
            row1.createCell(11).setCellValue(String.valueOf(parameterInfo.getCreateTime()));
            row1.createCell(12).setCellValue(String.valueOf(parameterInfo.getUpdateTime()));
            row1.createCell(13).setCellValue(String.valueOf(parameterInfo.getCreateUser().getId()));
            row1.createCell(14).setCellValue(parameterInfo.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("indexData_save")
    @ResponseBody
    public Result indexDataSave() {
        return null;
    }


    //过程告警
    @RequestMapping("warn_list")
    public void warnList(Model model) {

        /******条件搜索所需******/
        //适用对象
        List<Parameter> objNames = parameterService.findChildByParentCode("1010");
        for (Parameter pa : objNames) {
        }
        objNames.remove(0);
        model.addAttribute("objNames", objNames);
        //source
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("sources", sources);

    }

    @RequestMapping("warn_data")
    @ResponseBody
    public PageDto warnData(int pageNum, HttpServletRequest request) {
        Page<AlarmInfo> page = alarmInfoService.findAllAlarmInfo(pageNum, request);
        List<AlarmInfo> alarmInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(alarmInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("warn_add")
    public void warnAdd(Model model) {
        //适用对象
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        for (Parameter pa : objects) {
        }
        objects.remove(0);
        model.addAttribute("objects", objects);
        //所属类别
        List<TargetManagement> objTypes = new ArrayList<>();
        TargetManagement objType = new TargetManagement();
        objType.setId(1);
        objType.setName("电池故障");
        objTypes.add(objType);
        TargetManagement objType2 = new TargetManagement();
        objType2.setId(2);
        objType2.setName("变流电故障");
        objTypes.add(objType2);
        model.addAttribute("objTypes", objTypes);
        //来源
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("sources", sources);
        //报警等级
        List<TargetManagement> alarmLev = new ArrayList<>();
        TargetManagement lev = new TargetManagement();
        lev.setId(1);
        lev.setName("一级");
        alarmLev.add(lev);
        TargetManagement lev2 = new TargetManagement();
        lev2.setId(2);
        lev2.setName("二级");
        alarmLev.add(lev2);
        TargetManagement lev3 = new TargetManagement();
        lev3.setId(3);
        lev3.setName("三级");
        alarmLev.add(lev3);
        TargetManagement lev4 = new TargetManagement();
        lev4.setId(4);
        lev4.setName("四级");
        alarmLev.add(lev4);
        TargetManagement lev5 = new TargetManagement();
        lev5.setId(5);
        lev5.setName("无");
        alarmLev.add(lev5);
        model.addAttribute("alarmLev", alarmLev);

        List<TargetManagement> mode = new ArrayList<>();
        TargetManagement dispose = new TargetManagement();
        dispose.setId(1);
        dispose.setName("建议现场处理");
        mode.add(dispose);
        TargetManagement dispose2 = new TargetManagement();
        dispose2.setId(2);
        dispose2.setName("建议通知工程师");
        mode.add(dispose2);
        TargetManagement dispose3 = new TargetManagement();
        dispose3.setId(3);
        dispose3.setName("建议关闭系统");
        mode.add(dispose3);
        TargetManagement dispose4 = new TargetManagement();
        dispose4.setId(4);
        dispose4.setName("建议跟踪观察");
        mode.add(dispose4);
        model.addAttribute("mode", mode);


        //关联测点
        List<ParameterInfo> pcode = parameterInfoService.findCode();
        model.addAttribute("pcode", pcode);
        //阈值code
        List<ThresholdManage> tsm = thresholdService.findCode();
        model.addAttribute("tsm", tsm);

    }

    //查询code
    @RequestMapping("warn_code")
    @ResponseBody
    public String warnVerfiyCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<AlarmInfo> list = alarmInfoService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    @RequestMapping("warn_edit")
    public void warnEdit(String id, Model model) {
        AlarmInfo alarmInfo = alarmInfoService.findById(id);
        model.addAttribute("alarmInfo", alarmInfo);
        warnAdd(model);

    }

    @RequestMapping("warn_save")
    @ResponseBody
    public Result warnSave(String id, String code, String name, Integer objName, Integer objType, Integer source, Integer alarmLev,
                           Integer manageWay, Integer isRecordSoe, Integer soeDuration, Integer isShow, String parameterCode, String operator,
                           String thresholdCode, String alarmCanName) {
        alarmInfoService.saveAlarmInfo(id, code, name, objName, objType, source, alarmLev, manageWay, isRecordSoe, soeDuration,
                isShow, parameterCode, operator, thresholdCode, alarmCanName);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    @RequestMapping("warn_del")
    @ResponseBody
    public Result warnDel(String[] ids) {
        alarmInfoService.delParameterInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }


    @RequestMapping("warn_importData")
    public void warnImportData(Model model) {

    }

    /**
     * 下载模板
     */
    @RequestMapping("warn_exportTemplate")
    public void warn_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "alarmInfoTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "适用对象", "所属类别", "来源", "默认报警等级", "建议处理方式", "是否需要事故记录SOE", "SOE前后时长",
                "是否展示", "关联测点", "运算符", "关联阈值"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:C001");
        row1.createCell(1).setCellValue("例:单体欠压三级/SOC误差偏大...");
        row1.createCell(2).setCellValue("例:1:项目,2:电池包,3:电池模组,4:变流器,5:电池单体(只需给出数字)");
        row1.createCell(3).setCellValue("例:1:电池故障,2:变流电故障(只需给出数字)");
        row1.createCell(4).setCellValue("例:1:计算,2:诊断,3:通信(只需给出数字)");
        row1.createCell(5).setCellValue("例:1:一级,2:二级,3:三级,4:四级,5:无(只需给出数字)");
        row1.createCell(6).setCellValue("例:1:建立现场处理,2:建议通知工程师,3:建议关闭系统,4:建议跟踪观察(只需给出数字)");
        row1.createCell(7).setCellValue("例:0:不记录,1:记录(只需给出数字)");
        row1.createCell(8).setCellValue("例:23");
        row1.createCell(9).setCellValue("例:0:不展示,1:展示(只需给出数字)");
        row1.createCell(10).setCellValue("例:参数测点的编号,如C001");
        row1.createCell(11).setCellValue("例:>,<,=,≥,≤");
        row1.createCell(12).setCellValue("例:阈值管理的编号,如Y001");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    /**
     * 导入数据
     */

    @RequestMapping("warnImport_Data")
    @ResponseBody
    public String warnImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return alarmInfoService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;

    }

    /**
     * 导出数据
     */
    @RequestMapping("warn_exportData")
    public void warnExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<AlarmInfo> alarmInfos = alarmInfoService.findAllAlarmInfos();
        //设置要导出的文件的名字
        String fileName = "alarmInfo" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "名称", "适用对象", "所属类别", "来源", "默认报警等级", "建议处理方式", "是否需要事故记录SOE", "SOE前后时长",
                "是否展示", "关联测点", "运算符", "关联阈值", "创建时间", "修改时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (AlarmInfo alarmInfo : alarmInfos) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(alarmInfo.getId());
            row1.createCell(1).setCellValue(alarmInfo.getCode());
            row1.createCell(2).setCellValue(alarmInfo.getName());
            row1.createCell(3).setCellValue(alarmInfo.getObjName());
            row1.createCell(4).setCellValue(alarmInfo.getObjType());
            row1.createCell(5).setCellValue(alarmInfo.getSource());
            row1.createCell(6).setCellValue(alarmInfo.getAlarmLev());
            row1.createCell(7).setCellValue(alarmInfo.getManageWay());
            row1.createCell(8).setCellValue(alarmInfo.getIsRecordSoe());
            if (alarmInfo.getSoeDuration() == null) {
                row1.createCell(9).setCellValue("");
            } else {
                row1.createCell(9).setCellValue(alarmInfo.getSoeDuration());
            }

            row1.createCell(10).setCellValue(alarmInfo.getIsShow());
            row1.createCell(11).setCellValue(alarmInfo.getParameterCode());
            row1.createCell(12).setCellValue(alarmInfo.getOperator());
            row1.createCell(13).setCellValue(alarmInfo.getThresholdCode());
            row1.createCell(14).setCellValue(String.valueOf(alarmInfo.getCreateTime()));
            row1.createCell(15).setCellValue(String.valueOf(alarmInfo.getUpdateTime()));
            row1.createCell(16).setCellValue(alarmInfo.getCreateUser().getId());
            row1.createCell(17).setCellValue(alarmInfo.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }


    //设备状态
    @RequestMapping("status_list")
    public void statusList(Model model) {

        List<Parameter> objNames = parameterService.findChildByParentCode("1010");
        model.addAttribute("objNames", objNames);

        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("sources", sources);


    }

    @RequestMapping("status_data")
    @ResponseBody
    public PageDto statusData(int pageNum, HttpServletRequest request) {
        Page<EquipmentInfo> page = equipmentInfoService.findAllEquipmentInfo(pageNum, request);
        List<EquipmentInfo> equipmentInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(equipmentInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    @RequestMapping("status_add")
    public void statusAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
//        for (Parameter par : objects) {
//        }
//        Parameter pr = objects.get(2);
//        pr.setRemark("3");
//        objects.remove(0);
//        objects.remove(0);
//        objects.remove(3);
        model.addAttribute("objects", objects);
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("sources", sources);

    }

    //查询code
    @RequestMapping("status_code")
    @ResponseBody
    public String statusVerfiyCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<EquipmentInfo> list = equipmentInfoService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    @RequestMapping("status_del")
    @ResponseBody
    public Result statusDel(String[] ids) {
        equipmentInfoService.delEquipmentInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    //编辑
    @RequestMapping("status_edit")
    public void statusEdit(String id, Model model) {

        EquipmentInfo equipmentInfo = equipmentInfoService.findById(id);
        //回显状态
        String status = equipmentInfo.getStatus().replaceAll("0=", "").replaceAll("1=", "").replaceAll("2=", "")
                .replaceAll("3=", "").replaceAll("[|]", "");
        if (status.length() == 2) {
            String one = status.substring(0, 2);
            model.addAttribute("one", one);
        }
        if (status.length() == 4) {
            String one = status.substring(0, 2);
            model.addAttribute("one", one);

            String two = status.substring(2, 4);
            model.addAttribute("two", two);
        }
        if (status.length() == 6) {
            String one = status.substring(0, 2);
            model.addAttribute("one", one);

            String two = status.substring(2, 4);
            model.addAttribute("two", two);

            String three = status.substring(4, 6);
            model.addAttribute("three", three);
        }
        if (status.length() == 8) {
            String one = status.substring(0, 2);
            model.addAttribute("one", one);

            String two = status.substring(2, 4);
            model.addAttribute("two", two);

            String three = status.substring(4, 6);
            model.addAttribute("three", three);

            String four = status.substring(6, 8);
            model.addAttribute("four", four);
        }

        model.addAttribute("equipmentInfo", equipmentInfo);
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        for (Parameter par : objects) {
        }
        objects.remove(0);
        objects.remove(0);
        objects.remove(3);
        model.addAttribute("objects", objects);
        List<Parameter> sources = parameterService.findChildByParentCode("1020");
        model.addAttribute("sources", sources);

    }

    @RequestMapping("status_save")
    @ResponseBody
    public Result statusSave(String id, String code, String name, Integer objName, Integer source, String status, String statusExpression, Integer isShow) {
        equipmentInfoService.saveEquipmentInfo(id, code, name, objName, source, status, statusExpression, isShow);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;

    }

    /**
     * 导出数据
     */
    @RequestMapping("status_exportData")
    public void statusExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<EquipmentInfo> equipmentInfos = equipmentInfoService.findAllEquipmentInfos();
        //设置要导出的文件的名字
        String fileName = "equipmentInfo" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "名称", "各种状态", "状态表达式", "适用对象", "来源", "是否展示", "开始时间", "更新时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (EquipmentInfo equipmentInfo : equipmentInfos) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(equipmentInfo.getId());
            row1.createCell(1).setCellValue(equipmentInfo.getCode());
            row1.createCell(2).setCellValue(equipmentInfo.getName());
            row1.createCell(3).setCellValue(equipmentInfo.getStatus());
            row1.createCell(4).setCellValue(equipmentInfo.getStatusExpression());
            row1.createCell(5).setCellValue(equipmentInfo.getObjName());
            row1.createCell(6).setCellValue(equipmentInfo.getSource());
            row1.createCell(7).setCellValue(equipmentInfo.getIsShow());
            row1.createCell(8).setCellValue(String.valueOf(equipmentInfo.getCreateTime()));
            row1.createCell(9).setCellValue(String.valueOf(equipmentInfo.getUpdateTime()));
            row1.createCell(10).setCellValue(equipmentInfo.getCreateUser().getId());
            row1.createCell(11).setCellValue(equipmentInfo.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("status_importData")
    public void statusImportData(Model model) {

    }

    /**
     * 模板下载
     */
    @RequestMapping("status_exportTemplate")
    public void status_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "EquipmentInfoTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "各种状态", "状态表达式", "适用对象", "来源", "是否展示"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:Z001");
        row1.createCell(1).setCellValue("例:工作状态/交流断路器...");
        row1.createCell(2).setCellValue("例:0=A|1=B|2=C(A,B,C表示状态)");
        row1.createCell(3).setCellValue("例:0,1,2,3(根据状态对应的数字)");
        row1.createCell(4).setCellValue("例:1:项目,2:电池包,3:变流器(只需给出数字)");
        row1.createCell(5).setCellValue("例:1:计算,2:诊断,3:通信(只需给出数字)");
        row1.createCell(6).setCellValue("例:0:不展示,1:展示(只需给出数字)");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("statusImport_Data")
    @ResponseBody
    public String statusImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return equipmentInfoService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;
    }

    @RequestMapping("statusData_save")
    @ResponseBody
    public Result statusDataSave() {
        return null;
    }

    //台账属性
    @RequestMapping("property_list")
    public void propertyList(Model model) {
        List<TargetManagement> objects = new ArrayList<>();
        TargetManagement objType = new TargetManagement();
        objType.setId(1);
        objType.setName("项目");
        objects.add(objType);
        TargetManagement objType2 = new TargetManagement();
        objType2.setId(2);
        objType2.setName("PCS");
        objects.add(objType2);
        TargetManagement objType3 = new TargetManagement();
        objType3.setId(3);
        objType3.setName("电池包");
        objects.add(objType3);
        model.addAttribute("objects", objects);

        //类型
        List<TargetManagement> propertyTypes = new ArrayList<>();
        TargetManagement propertyType1 = new TargetManagement();
        propertyType1.setId(1);
        propertyType1.setName("整型");
        propertyTypes.add(propertyType1);
        TargetManagement propertyType2 = new TargetManagement();
        propertyType2.setId(2);
        propertyType2.setName("浮点型");
        propertyTypes.add(propertyType2);
        TargetManagement propertyType3 = new TargetManagement();
        propertyType3.setId(3);
        propertyType3.setName("字符型");
        propertyTypes.add(propertyType3);
        TargetManagement propertyType4 = new TargetManagement();
        propertyType4.setId(4);
        propertyType4.setName("日期型");
        propertyTypes.add(propertyType4);
        TargetManagement propertyType5 = new TargetManagement();
        propertyType5.setId(5);
        propertyType5.setName("图片型");
        propertyTypes.add(propertyType5);
        TargetManagement propertyType6 = new TargetManagement();
        propertyType6.setId(6);
        propertyType6.setName("表格型");
        propertyTypes.add(propertyType6);
        model.addAttribute("propertyTypes", propertyTypes);

    }

    @RequestMapping("property_data")
    @ResponseBody
    public PageDto propertyData(int pageNum, HttpServletRequest request) {
        Page<AccountProperty> page = accountPropertyService.findAllAccoutProperty(pageNum, request);
        List<AccountProperty> accountPropertieInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(accountPropertieInfos);
        pageDto.setTotal(total);
        return pageDto;
    }


    @RequestMapping("property_add")
    public void propertyAdd(Model model) {

        propertyList(model);
        //所属类别
        List<TargetManagement> parameterTypes = new ArrayList<>();
        List<TargetManagement> parameterTypes2 = new ArrayList<>();
        List<TargetManagement> parameterTypes3 = new ArrayList<>();
        TargetManagement property = new TargetManagement();
        property.setId(1);
        property.setName("项目信息");
        parameterTypes.add(property);
        TargetManagement property2 = new TargetManagement();
        property2.setId(2);
        property2.setName("电池包信息");
        parameterTypes3.add(property2);
        TargetManagement property3 = new TargetManagement();
        property3.setId(3);
        property3.setName("PCS信息");
        parameterTypes2.add(property3);
        TargetManagement property4 = new TargetManagement();
        property4.setId(4);
        property4.setName("系统信息");
        parameterTypes.add(property4);
        parameterTypes2.add(property4);
        TargetManagement property5 = new TargetManagement();
        property5.setId(5);
        property5.setName("BMS信息");
        parameterTypes3.add(property5);
        TargetManagement property6 = new TargetManagement();
        property6.setId(6);
        property6.setName("电价信息");
        parameterTypes.add(property6);
        model.addAttribute("parameterTypes", parameterTypes);
        model.addAttribute("parameterTypes2", parameterTypes2);
        model.addAttribute("parameterTypes3", parameterTypes3);

    }

    //查询code
    @RequestMapping("property_code")
    @ResponseBody
    public String propertyVerfiyPcode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<AccountProperty> list = accountPropertyService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    @RequestMapping("property_del")
    @ResponseBody
    public Result propertyDel(String[] ids) {
        accountPropertyService.delAccountPropertyInfo(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    @RequestMapping("property_edit")
    public void propertyEdit(String id, Model model) {
        AccountProperty accountPropertyInfo = accountPropertyService.findById(id);
        model.addAttribute("accountPropertyInfo", accountPropertyInfo);

        propertyAdd(model);

    }

    @RequestMapping("property_save")
    @ResponseBody
    public Result propertySave(String id, String code, String name, Integer objName, Integer parameterType, Integer propertyType, Integer isModify) {
        accountPropertyService.saveAccountPropertyInfo(id, code, name, objName, parameterType, propertyType, isModify);

        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }


    @RequestMapping("propertyData_save")
    @ResponseBody
    public Result propertyDataSave() {
        return null;
    }

    /**
     * 导出数据
     */
    @RequestMapping("property_exportData")
    public void propertyExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<AccountProperty> accountProperties = accountPropertyService.findAllAccoutPropertyInfo();
        //设置要导出的文件的名字
        String fileName = "accountPropertyInfo" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "名称", "适用对象", "所属类别", "类型", "是否可修改", "创建时间", "更新时间", "创建人ID", "是否有效"};

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (AccountProperty accountProperty : accountProperties) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(accountProperty.getId());
            row1.createCell(1).setCellValue(accountProperty.getCode());
            row1.createCell(2).setCellValue(accountProperty.getName());
            row1.createCell(3).setCellValue(accountProperty.getObjName());
            row1.createCell(4).setCellValue(accountProperty.getParameterType());
            row1.createCell(5).setCellValue(accountProperty.getPropertyType());
            row1.createCell(6).setCellValue(accountProperty.getIsModify());
            row1.createCell(7).setCellValue(String.valueOf(accountProperty.getCreateTime()));
            row1.createCell(8).setCellValue(String.valueOf(accountProperty.getUpdateTime()));
            row1.createCell(9).setCellValue(accountProperty.getCreateUser().getId());
            row1.createCell(10).setCellValue(accountProperty.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //显示模态框
    @RequestMapping("property_importData")
    public void propertyImportData(Model model) {

    }

    /**
     * 模板下载
     */
    @RequestMapping("property_exportTemplate")
    public void property_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "AccountPropertyTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "适用对象", "所属类别", "类型", "是否可二次修改"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:T001");
        row1.createCell(1).setCellValue("例:项目名称/地址/经度/纬度...");
        row1.createCell(2).setCellValue("例:1:项目,2:PCS,3:电池包(只需给出数字)");
        row1.createCell(3).setCellValue("例:1:项目信息,2:电池包信息,3:PCS信息,4:系统信息,5:电价信息(只需给出数字)");
        row1.createCell(4).setCellValue("例:1:整型,2:浮点型,3:字符型,4:日期型,5:图片型,6:表格型(只需给出数字)");
        row1.createCell(5).setCellValue("例:0:否,1:是(只需给出数字)");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //导入
    @RequestMapping("propertyImport_Data")
    @ResponseBody
    public String propertyImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return accountPropertyService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;
    }

    //控制指令
    @RequestMapping("order_list")
    public void orderList(Model model) {
        List<Parameter> objNames = parameterService.findChildByParentCode("1010");
        model.addAttribute("objNames", objNames);
    }

    @RequestMapping("order_data")
    @ResponseBody
    public PageDto orderData(int pageNum, HttpServletRequest request) {
        Page<ControlOrder> page = controlOrderService.findAllControlOrder(pageNum, request);
        List<ControlOrder> controlOrdersInfo = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(controlOrdersInfo);
        pageDto.setTotal(total);
        return pageDto;
    }

    //添加
    @RequestMapping("order_add")
    public void orderAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        model.addAttribute("objects", objects);
    }

    @RequestMapping("order_del")
    @ResponseBody
    public Result orderDel(String[] ids) {
        controlOrderService.delControlOrderInfos(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    @RequestMapping("order_edit")
    public void orderEdit(String id, Model model) {
        ControlOrder controlOrder = controlOrderService.findById(id);
        model.addAttribute("controlOrder", controlOrder);
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        model.addAttribute("objects", objects);

    }

    /**
     * 编号的校验
     *
     * @param req
     * @return
     */
    @RequestMapping("order_code")
    @ResponseBody
    public String orderCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<ControlOrder> list = controlOrderService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    //查询code
    @RequestMapping("order_save")
    @ResponseBody
    public Result orderSave(String id, String code, String name, Integer objName, Integer isControl) {

        controlOrderService.saveControlOrderInfo(id, code, name, objName, isControl);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 导出数据
     */
    @RequestMapping("order_exportData")
    public void orderExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<ControlOrder> controlOrders = controlOrderService.findAllEquipmentInfos();
        //设置要导出的文件的名字
        String fileName = "controlOrderInfo" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "名称", "适用对象", "是否可手动操作", "创建时间", "更新时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (ControlOrder controlOrder : controlOrders) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(controlOrder.getId());
            row1.createCell(1).setCellValue(controlOrder.getCode());
            row1.createCell(2).setCellValue(controlOrder.getName());
            row1.createCell(3).setCellValue(controlOrder.getObjName());
            row1.createCell(4).setCellValue(controlOrder.getIsControl());
            row1.createCell(5).setCellValue(String.valueOf(controlOrder.getCreateTime()));
            row1.createCell(6).setCellValue(String.valueOf(controlOrder.getUpdateTime()));
            row1.createCell(7).setCellValue(controlOrder.getCreateUser().getId());
            row1.createCell(8).setCellValue(controlOrder.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //显示模态框
    @RequestMapping("order_importData")
    public void orderImportData(Model model) {

    }

    /**
     * 模板下载
     */
    @RequestMapping("order_exportTemplate")
    public void order_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "ControlOrderTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "适用对象", "是否可手动操作"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:K001");
        row1.createCell(1).setCellValue("例:打开正极继电器");
        row1.createCell(2).setCellValue("例:1:项目,2:电池包,3:电池模组,4:变流器,5:电池单体,6:平台(只需给出数字)");
        row1.createCell(3).setCellValue("例:0:否,1:是(只需给出数字)");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //导入数据
    @RequestMapping("orderImport_Data")
    @ResponseBody
    public String orderImportSave(MultipartFile myFile, String flag) throws Exception {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return controlOrderService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;

    }

    //阈值管理
    @RequestMapping("threshold_list")
    public void shresholdList(Model model) {
        TargetManagement obj = new TargetManagement();
        List objects = new ArrayList();
        obj.setId(1);
        obj.setName("变流器");
        objects.add(obj);
        TargetManagement obj2 = new TargetManagement();
        obj2.setId(2);
        obj2.setName("电池包");
        objects.add(obj2);
        model.addAttribute("objects", objects);

    }

    @RequestMapping("threshold_data")
    @ResponseBody
    public PageDto shresholdData(int pageNum, HttpServletRequest request) {
        Page<ThresholdManage> page = thresholdService.findAllThreshold(pageNum, request);
        List<ThresholdManage> thresholdManages = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(thresholdManages);
        pageDto.setTotal(total);
        return pageDto;

    }

    @RequestMapping("threshold_add")
    public void thresholdAdd(Model model) {
        shresholdList(model);

    }

    /**
     * code的校验
     *
     * @param req
     * @return
     */
    @RequestMapping("threshold_code")
    @ResponseBody
    public String thresholdCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<ThresholdManage> list = thresholdService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    //回显
    @RequestMapping("threshold_edit")
    public void thresholdEdit(String id, Model model) {
        ThresholdManage thresholdInfo = thresholdService.findById(id);
        model.addAttribute("thresholdInfo", thresholdInfo);
        thresholdAdd(model);
    }

    //保存
    @RequestMapping("threshold_save")
    @ResponseBody
    public Result orderDataSave(String id, String code, String name, Integer objName, String source,
                                Integer isModify, String sysDefVal, String unit) {
        thresholdService.saveThreshold(id, code, name, objName, source, isModify, sysDefVal, unit);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    //删除
    @RequestMapping("threshold_del")
    @ResponseBody
    public Result thresholdDel(String[] ids) {
        thresholdService.delThresholdManage(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    /**
     * 导出数据
     */
    @RequestMapping("threshold_exportData")
    public void thresholdExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<ThresholdManage> thresholdInfos = thresholdService.findAllThresholdManageInfos();
        //设置要导出的文件的名字
        String fileName = "thresholdManage" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "适用对象", "来源", "是否可修改", "系统默认值", "单位", "创建时间", "更新时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (ThresholdManage thresholdManage : thresholdInfos) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(thresholdManage.getId());
            row1.createCell(1).setCellValue(thresholdManage.getCode());
            row1.createCell(2).setCellValue(thresholdManage.getName());
            row1.createCell(3).setCellValue(thresholdManage.getObjName());
            row1.createCell(4).setCellValue(thresholdManage.getSource());
            row1.createCell(5).setCellValue(thresholdManage.getIsModify());
            row1.createCell(6).setCellValue(thresholdManage.getSysDefVal());
            row1.createCell(7).setCellValue(thresholdManage.getUnit());
            row1.createCell(8).setCellValue(String.valueOf(thresholdManage.getCreateTime()));
            row1.createCell(9).setCellValue(String.valueOf(thresholdManage.getUpdateTime()));
            row1.createCell(10).setCellValue(thresholdManage.getCreateUser().getId());
            row1.createCell(11).setCellValue(thresholdManage.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //显示模态框
    @RequestMapping("threshold_importData")
    public void thresholdImportData(Model model) {

    }

    /**
     * 模板下载
     */
    @RequestMapping("threshold_exportTemplate")
    public void threshold_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "ThresholdManageTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "名称", "适用对象", "来源", "是否可修改", "系统默认值", "单位"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:Y001");
        row1.createCell(1).setCellValue("例:电池包过压一级阈值");
        row1.createCell(2).setCellValue("例:1:项目,2:电池包,3:电池模组,4:变流器,5:电池单体,6:平台(只需给出数字)");
        row1.createCell(3).setCellValue("例:1:计算,2:诊断,3:通信(可多选,只需给出数字)");
        row1.createCell(4).setCellValue("例:0:否,1:是(只需给出数字)");
        row1.createCell(5).setCellValue("例:3550(系统默认)");
        row1.createCell(6).setCellValue("例:V/℃...");

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //导入数据
    @RequestMapping("thresholdImport_Data")
    @ResponseBody
    public String thresholdImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return thresholdService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;

    }

    //类别管理
    @RequestMapping("type_list")
    public void typeList(Model model) {
        System.out.println(1111);
    }

    @RequestMapping("type_data")
    @ResponseBody
    public PageDto typeData(int pageNum, HttpServletRequest request) {
        Page<CategoryManage> page = categoryManageService.findAllCategory(pageNum, request);
        List<CategoryManage> categoryManages = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(categoryManages);
        pageDto.setTotal(total);
        return pageDto;
    }

    //添加
    @RequestMapping("type_add")
    public void typeAdd(Model model) {
        List<TargetManagement> target = new ArrayList<>();
        TargetManagement type1 = new TargetManagement();
        type1.setId(1);
        type1.setName("参数测点");
        target.add(type1);
        TargetManagement type2 = new TargetManagement();
        type2.setId(2);
        type2.setName("过程报警");
        target.add(type2);
        TargetManagement type3 = new TargetManagement();
        type3.setId(3);
        type3.setName("设备状态");
        target.add(type3);
        TargetManagement type4 = new TargetManagement();
        type4.setId(4);
        type4.setName("台账属性");
        target.add(type4);
        TargetManagement type5 = new TargetManagement();
        type5.setId(5);
        type5.setName("控制指令");
        target.add(type5);
        TargetManagement type6 = new TargetManagement();
        type6.setId(6);
        type6.setName("阈值管理");
        target.add(type6);
        model.addAttribute("target", target);

    }

    //查询code
    @RequestMapping("type_code")
    @ResponseBody
    public String categoryManageCode(HttpServletRequest req) {
        //获取code
        String name = req.getParameter("code");
        if (name != null && StringUtils.isNotBlank(name)) {
            List<CategoryManage> list = categoryManageService.findCode();
            if (list.contains(name)) {
                return "编号已存在";
            }
            return "";
        }

        return null;
    }

    //编辑
    @RequestMapping("type_edit")
    public void typeEdit(String id, Model model) {
        CategoryManage categoryInfo = categoryManageService.findById(id);
        model.addAttribute("categoryInfo", categoryInfo);
        typeAdd(model);

    }

    //保存
    @RequestMapping("type_save")
    @ResponseBody
    public Result typeDataSave(String id, String code, String name, String objName, String useTarget) {
        categoryManageService.saveCategoryManageInfo(id, code, name, objName, useTarget);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    //删除
    @RequestMapping("type_del")
    @ResponseBody
    public Result typeDel(String[] ids) {
        categoryManageService.delCategoryManageInfos(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

    /**
     * 模板下载
     */
    @RequestMapping("type_exportTemplate")
    public void type_exportTemplate(HttpServletResponse response) throws IOException {
        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet并命名
        HSSFSheet sheet = workbook.createSheet("模板样式");
        //设置要导出的文件的名字
        String fileName = "CategoryManageTemplate" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"编号", "类别名称", "适用对象", "使用指标"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue("例:L001");
        row1.createCell(1).setCellValue("例:能量/电流/电压...");
        row1.createCell(2).setCellValue("例:1:项目,2:电池包,3:电池模组,4:变流器,5:电池单体,6:平台(可多选,只需给出数字)");
        row1.createCell(3).setCellValue("例:1:参数测点,2:过程报警,3:设备状态,4:台账属性,5:控制指令,6:阈值管理(可多选,只需给出数字)");


        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    /**
     * 导出数据
     */
    @RequestMapping("type_exportData")
    public void typeExportData(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<CategoryManage> categoryManages = categoryManageService.findAllCategoryManageInfos();
        //设置要导出的文件的名字
        String fileName = "categoryManage" + ".xls";
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"ID", "编号", "类别名称", "适用对象", "适用指标", "创建时间", "更新时间", "创建人ID", "是否有效"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (CategoryManage categoryManage : categoryManages) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(categoryManage.getId());
            row1.createCell(1).setCellValue(categoryManage.getCode());
            row1.createCell(2).setCellValue(categoryManage.getName());
            row1.createCell(3).setCellValue(categoryManage.getObjName());
            row1.createCell(4).setCellValue(categoryManage.getUseTarget());
            row1.createCell(5).setCellValue(String.valueOf(categoryManage.getCreateTime()));
            row1.createCell(6).setCellValue(String.valueOf(categoryManage.getUpdateTime()));
            row1.createCell(7).setCellValue(categoryManage.getCreateUser().getId());
            row1.createCell(8).setCellValue(categoryManage.getIsValid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //显示模态框
    @RequestMapping("type_importData")
    public void categoryImportData(Model model) {

    }

    //导入
    @RequestMapping("typeImport_Data")
    @ResponseBody
    public String categoryImportSave(MultipartFile myFile, String flag) {
        //设置标记返回页面回显状态
        try {
            List<String[]> list = POIUtil.readExcel(myFile);
            if (list.size() != 0 && !list.contains(null)) {
                return categoryManageService.saveImport(list, flag);
            }
            flag = "1";
        } catch (IOException e) {
            e.printStackTrace();
            flag = "1";
        }
        return flag;

    }
}
